<?php
namespace app\admin\model;
use think\model;
class Brand  extends  Model
{
    protected $pk = 'brand_id';


    public function searchKeywordsAttr($query,$value,$data){
        if (''!==$value){
            $query->whereLike('brand_name','%'.$value.'%');
        }
    }
    public function searchRecommendedAttr($query,$value,$data)
    {
        if (is_array($value)||(is_scalar($value) && strpos('#'.$value,','))){
            $query->whereIn('ecm_brand.recommended',$value);
        }
        if (is_scalar($value) && false === strpos($value,',')&& intval($value) < 2){
            $query->where('ecm_brand.recommended',intval($value));
        }
    }
    public function searchLogoAttr($query,$value,$data)
    {
//        if (is_array($value)){
//            $query->whereIn('brand_logo',$value);
//        }
//        if (is_string($value) &&  strpos('#'.$value,',')){
//            $query->whereIn('brand_logo',$value);
//        }
//        if(is_string($value) &&  false === strpos($value,',')){
//            $query->where('brand_logo',$value);
//        }
//        if(is_array($value) &&  1 == count($value) && 1 == $value[0]){//有图
//            $query->where('brand_logo','not null')->where('brand_logo','<>','');
//        }
        if(is_array($value) &&  1== count($value) && 0 ==$value[0]){//有图
            $query->where(function ($q){
                $q->where('brand_logo',null)->whereOr('brand_logo','');
            });
        }

    }
    //构造器
//    public function searchPageAttr($query,$value){}
    public static function getBrand($data = [],$query= [],$page = false)
    {
        echo "<pre>";
        print_r($data);
        echo "</pre>";
        $model = self::field(true)
            ->withSearch(array_keys($data),$data);

//        if (isset($data['logo']) && is_array($data['logo'])){
//            $model->whereIn('brand_logo',$data['logo']);
//        }
//        if(isset($data['logo']) && is_string($data['logo']) &&  strpos('#'.$data['logo'],',')){
//            $model->whereIn('brand_logo',$data['logo']);
//        }
//
//        if(isset($data['logo']) && is_string($data['logo']) &&  false === strpos($data['logo'],',')){
//            $model->where('brand_logo',$data['logo']);
//        }
//        if(isset($data['logo'])&& is_array($data['logo']) &&  1 == count($data['logo']) && 1 == $data['logo'][0]){//有图
//            $model->where('brand_logo','not null')->where('brand_logo','<>','');
//        }
//        if(isset($data['logo'])&&is_array($data['logo']) &&  1== count($data['logo']) && 0 == $data['logo'][0]){//有图
//            $model->where(function ($q){
//                $q->where('brand_logo',null)->whereOr('brand_logo','');
//            });
//        }




        if ($page){
            $result = $model->select();
        }else{
            $result = $model->paginate([
               'list_rows' =>5,
               'query'     =>$query,
            ]);
        }
        return $result;
    }
    public static function BrandList()  //品牌分类
    {
        $model = self::field('brand_id,brand_name')->select();

        return $model;
    }
    public function add(array $data,array $allowField = [],$replace = false)
    {
        try{
            if(empty($data)){
                $this->error='数据不能为空';
            }
            $this->save($data);
            $id=$this->toArray();
            return $id[$this->pk];
        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }

}